package com.example.makerspace.manager;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.makerspace.dao.ExamDAO;
import com.example.makerspace.domain.ExamDO;
import com.example.makerspace.dto.*;
import com.example.makerspace.enums.ResultCodeEnum;
import com.example.makerspace.utils.FileUtils;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static com.example.makerspace.service.impl.AdminServiceImpl.managerMap;

@Component
public class ExamManager extends  BaseManager<ExamDO, ExamDTO>{
    private final static String EXAM_PICTURE_PATH_PREFIX = "exam/";

    private final static List<Map<String, String>> FUZZY_QUERY_PARAMS = Lists.newArrayList(
            ImmutableMap.<String, String>builder().put(QUERY_FIELD, "exam_name").put(QUERY_TYPE, QUERY_TYPE_MATCH_PHRASE).build(),
            ImmutableMap.<String, String>builder().put(QUERY_FIELD, "keyword").put(QUERY_TYPE, QUERY_TYPE_MATCH_PHRASE).build()
    );
  private  final  static  String   testHttpPrefix="https://119.45.199.13:3035/static/";

   private  final  static  String  devHttpPrefix="https://nism.ytcata.com:443/static/";
    @Resource
    private ExamDAO examDAO;

    @PostConstruct
    public void init(){
        this.baseDAO = examDAO;
        managerMap.put("exam", this);
    }

    public Result addExam(ExamDTO examDTO) {
        ExamDO oldExamDO = examDAO.selectByExamName(examDTO.getExamName());
        if(oldExamDO != null){
            return Result.error(ResultCodeEnum.EXAM_EXIST);
        }
        String imagePath = FileUtils.saveMultipartFile(examDTO.getPicture(), FileUtils.staticPath + EXAM_PICTURE_PATH_PREFIX + examDTO.getExamName());
        ExamDO examDO = new ExamDO();
        BeanUtils.copyProperties(examDTO, examDO);
        examDO.setPicture(imagePath);
        return addDO(examDO);
    }

    public Result updateExam(ExamDTO examDTO) {
        ExamDO examDO = examDAO.selectById(examDTO.getId());
        if(examDO == null){
            return Result.error(ResultCodeEnum.EXAM_NOT_EXIST);
        }
        Optional.ofNullable(examDTO.getExamName()).ifPresent(examDO::setExamName);
        Optional.ofNullable(examDTO.getLevel()).ifPresent(examDO::setLevel);
        Optional.ofNullable(examDTO.getKeyword()).ifPresent(examDO::setKeyword);
        Optional.ofNullable(examDTO.getPicture()).ifPresent(picture ->{
            FileUtils.deleteFile(examDO.getPicture());
            String imagePath = FileUtils.saveMultipartFile(picture, FileUtils.staticPath + EXAM_PICTURE_PATH_PREFIX + examDTO.getExamName());
            examDO.setPicture(imagePath);
        });
        Optional.of(examDTO.getPasserNum()).ifPresent(examDO::setPasserNum);
        return updateDO(examDO);
    }


    protected ExamDTO convert2DTO(ExamDO examDO) {
        ExamDTO examDTO = super.convert2DTO(examDO);
        String picture = examDO.getPicture();
        String briefPath = FileUtils.getBriefPath(picture);
        String path = devHttpPrefix+ briefPath;
        examDTO.setPicturePath(path);
        return examDTO;
    }

    @Override
    public PageResult list(PageRequest pageRequest){
        Map<String, Object> params = pageRequest.getParams();
        Pagination pagination = pageRequest.getPagination();
        String fuzzy = MapUtils.getString(params, "fuzzy");
        if(StringUtils.isBlank(fuzzy)){
            return super.list(pageRequest);
        }
        return search(null, null, FUZZY_QUERY_PARAMS, fuzzy, pagination, ExamDO.class);
    }
}
